
/*
 * RequestHandlerServlet.java *
 * Created on June 15, 2007, 6:23 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.*;

import utility.Input;

import database.RecordSearch;
import dbmanager.*;

/**
 * @author Hemant
 * This servlet Handles request for all types of HTML pages either dynamic or static.
 * It simply redirects to all static pages or generates HTML for dynamic pages.
 */
public class RequestHandlerServlet extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 511866877887683079L;
	
	HttpServletRequest request;
	HttpServletResponse response;
	String username = null,password = null;
	GetIdName get;
	PrintWriter out;
	
    /** Creates a new instance of RequestHandlerServlet */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        doPost(request,response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    {
    	this.request = request;
    	this.response = response;
    	String url = null;
        get = new GetIdName();
        HttpSession session = request.getSession();
        username = (String) session.getAttribute("username");
		password = (String) session.getAttribute("password");
		
    	guireportgenerator.RequestHandler requhand = new guireportgenerator.RequestHandler();
    	
    	try
    	{
            PrintWriter out = response.getWriter();            
            String objectname = (String)session.getAttribute("objectname"); 
            
            String page = (String)session.getAttribute("page");
			String process = (String)session.getAttribute("process");
			
			if(process == null)
				process = "view";			
			
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			Enumeration<Object> enum1 = request.getParameterNames();
			
					
			Vector<Object> property_vector = new Vector<Object>();
			String report = null;
									
			hash.put("process", process);
			hash.put("page", page);
			
			System.out.println("The ReqHand  "+username+"	"+page+"	"+objectname);
			
			while(enum1.hasMoreElements())
			{
				String param = (String)enum1.nextElement();
				
				if(!hash.containsKey(param))
				{
					String param_val = request.getParameter(param);
					hash.put(param, param_val);
				}
				
				System.out.println(hash);
			}
			
			Hashtable<Object, Object> property_hash = new Hashtable<Object, Object>();
			
			// Lets extract data report defined inside page  
			try 
			{
				Hashtable<Object, Object> condition = new Hashtable<Object, Object>();				
				condition.put("mid", get.getId(page));
				condition.put("pid", get.getId("report"));
				condition.put("td", "null");
				RecordSearch rec = new RecordSearch();
				rec.setConditionMap(condition);
				ResultSet rs = rec.getResultSet();
				
				while (rs.next())
				{
					report = get.getItem((String)rs.getString("PV"));
					rs.close();
					if(report == null)
						break;
					// This report will give us data in form a hashtable which we will
					//put in session in the form of vector 
					guireportgenerator.Report rep = new guireportgenerator.Report(report);
		            //rep.setDatabase();
		            rep.setGetIdName(get);
		            rep.setParamHash(hash);
		            property_hash = rep.getHash();
		            
		            enum1 = property_hash.keys();
		            while(enum1.hasMoreElements())
		            {
		            	String property = (String)enum1.nextElement();
		            	property_vector.add(property);
		            	String value = (String)property_hash.get(property);
		            	property_vector.add(value);
		            }
					if(!property_vector.isEmpty())
						session.setAttribute("property_list", property_vector);						
				}				
			} catch (Exception e) {
			}
			
			// Set params for RequestHandler class
			if(property_hash.isEmpty())
				property_hash.putAll(hash);
			requhand.setConditionHash(hash);
			requhand.setDataHash(property_hash);
			requhand.setUsername(username);
			requhand.setRequestPage(page);
			requhand.setMaster(objectname);
			requhand.setProcess(process);
			requhand.setRport(report);
			
			boolean isURL = requhand.isUrl();		
			
			// For HTTP
			if (isURL) {
				url = "http://" + Input.URL+ requhand.getFile();
				System.out.println("In URL "+url);
				out.write("<html><head>");
				out.write("<META HTTP-EQUIV='Refresh' CONTENT='0;URL="
						+ url + ">");
				out.write("</head><body></body></html>");
				out.flush();
			} else {
				url = requhand.getHtml();
				String temp_page = requhand.getPage();
				session.setAttribute("page", temp_page);
				out.write(url);
				out.flush();
			}
			
			/*// For FTP
			url = requhand.getFileContent(requhand.getFileName());
			out.write(url);
			out.flush();*/
       }
       catch(Exception e)
       {
    	   e.printStackTrace();
    	   
    	   try
    	   {
    		   response.sendRedirect("/poolweb/Logout");
    	   }
    	   catch (Exception e1)
    	   {
    		   e1.printStackTrace();    		  
    	   }
       }
       finally
       {
    	   out = null;
       }
    }
}

